import type { App } from "vue";
import { createMemoryHistory, RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import { message, notification } from "ant-design-vue";
import { dynamicRoutes } from "./routerLIst"

import CooieTool from "@/utils/cookie";
import Layout from "@/layout/layout.vue";
const constantRoutes: Array<RouteRecordRaw> = [
	// {
	// 	path: '/:catchAll(.*)',
	// 	redirect: '/home',
	// 	meta: {
	// 		menuHide: true
	// 	}
	// },
	{
		path: '/',
		name: 'home',
		component: Layout,
		redirect: 'home',
		meta: {
			title: '首页',
			menuHide: true, // 不显示在左侧菜单栏
			icon:'icon-Tab_shouye' 
		},
		children: [
			{
				path: 'home',
				name: 'Home',
				meta: { title: "首页",icon:'icon-Tab_shouye' },
				component: () => import('@/view/home/index.vue')
			}
		]
	},
	{
		path: "/login",
		name: "login",
		meta: { title: "登录",icon:'' },
		component: () => import('@/view/login/login.vue')
	},
	{
		path: "/index",
		component: Layout,
		meta: { title: "一级路由",icon:"icon-yonghu-yuan" },
		children: [
			{
				path: "/testindex",
				name: "testindex",
				meta: { title: "内容",icon:'icon-jigou' },
				component: () => import('@/view/exception/500.vue')
			},
			{
				path: '/user',
				name: 'user',
				meta: { title: '用户列表',icon:'icon-Tab_yonghuguanli', role: ['admin'] },
				component: () => import('@/view/exception/404.vue')
			},
			{
				path: '/drou',
				name: 'drou',
				meta: { title: '二级路由', role: ['admin'],icon:'icon-dangan' },
				children: [
					{
						path: '/userAdd',
						name: 'userAdd',
						meta: { title: '新增用户', role: ['admin'],icon:'icon-jurassic_add-user' },
						component: () => import('@/view/exception/200.vue')
					},
					{
						path: '/userEdit',
						name: 'userEdit',
						meta: { title: '三级路由', role: ['admin'],icon:'icon-jurassic_add-users' },
						children: [
							{
								path: '/user3',
								name: 'user3',
								meta: { title: '三级用户', role: ['admin'],icon:'icon-canshuguanli' },
								component: () => import('@/view/exception/500.vue')
							},
						]
					},
				]
			},
		]
	},
	{
		path: "/other",
		meta: { title: "新开窗口",icon:'icon-Tab_goumaifuwu' },
		component: Layout,
		redirect: "/other2",
		children: [
			{
				path: "/other2",
				name: "other2",
				meta: { title: "新开窗口",icon:'icon-Tab_goumaifuwu' },
				component: () => import('@/view/other/other.vue')
			}
		]
	},
	// 更新窗口
	{
		path: "/updates",
		meta: { title: "更新窗口",icon:'icon-Tab_shujujiankong' },
		component: Layout,
		redirect: "/update",
		children: [
			{
				path: "/update",
				name: "update",
				meta: { title: "更新窗口",icon:'icon-Tab_shujujiankong' },
				component: () => import('@/view/update/update.vue')
			}
		]
	},
]

const router = createRouter({
	// createWebHashHistory (hash路由 Hash模式 #)
	// createWebHistory (history路由 HTML5 模式 推荐，需要服务器配置支持)
	// createMemoryHistory 带缓存 history 路由
	history: createWebHashHistory(),
	routes: constantRoutes,
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition;
		} else {
			return { top: 0, behavior: "smooth" };
		}
	}
});
// 路由守卫
router.beforeEach((to, from, next) => {
	if (to.path !== from.path) {
		// document.title = `${to.meta.title}`;
		document.title = 'electronApp'
	}
	if (to.path === "/login") {
		localStorage.removeItem("Cusertoken")
		next();
	} else {
		if (to.path === "/other2") return next();
		const token = CooieTool.getCookie("Cusertoken");
		const usertoken = localStorage.getItem("Cusertoken");
		if (!usertoken) {
			next("/login")
			message.warning('登录过期，请重新登录', 3);
		};
		// if (token || tokenTow) return next("/login");
		next();
	}
});

export function setupRouter(app: App<Element>) {
	app.use(router);
}

export default router;
